/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bookshop.facade;

import com.bookshop.entity.Book;
import com.bookshop.entity.Category;
import com.bookshop.entity.Collection;
import com.bookshop.entity.Publisher;
import com.bookshop.entity.Serie;
import java.sql.SQLException;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Thibault
 */
@Stateless
public class BookFacade extends AbstractFacade<Book> {

    @PersistenceContext(unitName = "BookShop-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public BookFacade() {
        super(Book.class);
    }

    @Override
    public List<Book> findAll() {
        Query query = em.createNamedQuery("Book.findAll");
        return query.getResultList();
    }

    public List<Book> findByTitle(String title) {
        Query query = em.createNamedQuery("Book.findByTitle");
        query.setParameter("title", "%" + title + "%");
        return query.getResultList();
    }
    public List<Book> findByCategory(int categoryID) {
        Query query2 = em.createNamedQuery("Book.findByCategoryId");
        query2.setParameter("categoryId", new Category(categoryID));
        return query2.getResultList();
    }

    /* ********************************************************  */
    /*                        code ajouté                        */
    /* ********************************************************** */
    
    public Book getBookByID(String isbn) throws SQLException {
        String requet = "Select b from Book b where b.isbn= :pid";
        Query query = em.createQuery(requet);
        query.setParameter("pid", isbn);
        Book book = (Book) query.getSingleResult();
        return book;
    }
//
//    public String getResumeByID(String isbn) throws SQLException {
//        String resume = this.getBookByID(isbn).getSummary();
//        return resume;
//    }
//
//    public String getTitreByID(String isbn) throws SQLException {
//        String titre = this.getBookByID(isbn).getTitle();
//        return titre;
//    }
//
//    public String getImageeByID(String isbn) throws SQLException {
//        String imageurl = this.getBookByID(isbn).getBitmapURL();
//        return imageurl;
//    }
//
//    public double getPrixByID(String isbn) throws SQLException {
//        double prix = this.getBookByID(isbn).getPrice();
//        return prix;
//    }
//
//    public String getAuteurByID(int idAuthor) throws SQLException {
//        String requet2 = "Select a from Author a where a.id= :pid";
//        Query query = em.createQuery(requet2);
//        query.setParameter("pid", idAuthor);
//        Author auteur = (Author) query.getSingleResult();
//        String fullName = auteur.getFirstName() + " " + auteur.getLastName();
//        return fullName;
//    }

    public String getCategorieByID(String isbn) throws SQLException {
        Category categorie = this.getBookByID(isbn).getCategoryid();
        String nomCategorie = categorie.getLabel();
        return nomCategorie;
    }

    public String getSerieByID(String isbn) throws SQLException {
        String nomSerie = null;
        Serie serie = this.getBookByID(isbn).getSeriesid();
        nomSerie = serie.getLabel();
        return nomSerie;
    }

    public String getDateParutionByID(String isbn) throws SQLException {
        String date = this.getBookByID(isbn).getPublicationDate();
        return date;
    }

    public String getCollectionByID(String isbn) throws SQLException {
        String nomCollection = null;
        List<Collection> collections = this.getBookByID(isbn).getCollectionList();
        if (!collections.isEmpty()) {
            for (Collection c : collections) {
                if (c.getLabel() != null) {
                    nomCollection = c.getLabel() + " ";
                }
            }
        }
        return nomCollection;
    }

    public String getEditeurByID(String isbn) throws SQLException {
        String nomEditeur = null;
        List<Publisher> editeurs = this.getBookByID(isbn).getPublisherList();
        if (!editeurs.isEmpty()) {
            for (Publisher p : editeurs) {
                if (p.getLabel() != null) {
                    nomEditeur = p.getLabel() + " ";
                }
            }
        }
        return nomEditeur;
    }
}
